/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frontera;

import entidad.Lote;
import entidad.Producto;
import entidad.Proveedor;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractListModel;

/**
 *
 * @author Andrés,Daniel,Osmond
 */
public class VisualizarProductos extends javax.swing.JPanel {

    /**
     * Creates new form VisualizarProducto
     */
    private AdministarProductos administrarProductos;
    private List<Lote> listaLotes;
    private List<Proveedor> listaProveedores;

    public VisualizarProductos(AdministarProductos administrarProductos) {
        initComponents();
        this.administrarProductos = administrarProductos;
        limpiar();
        deshabilitarTodo();
        mostrarAviso("");
    }

    public AdministarProductos getAdministarProductos() {
        return administrarProductos;
    }

    public void deshabilitarTodo() {
        codigoTF.setEditable(false);
        nombreTF.setEditable(false);
        marcaTF.setEditable(false);

        ProveedoresJL.setEnabled(false);
        agregarProveedorB.setVisible(false);
        eliminarProveedorB.setVisible(false);

        LotesJL.setEnabled(false);

        cancelarB.setVisible(false);
        guardarB.setVisible(false);
    }

    public void habilitarTodo() {
        codigoTF.setEditable(true);
        nombreTF.setEditable(true);
        marcaTF.setEditable(true);

        ProveedoresJL.setEnabled(true);
        agregarProveedorB.setVisible(true);
        eliminarProveedorB.setVisible(true);

        LotesJL.setEnabled(true);

        mostrarAviso("");

        cancelarB.setVisible(true);
        guardarB.setVisible(true);
    }

    public void habilitarModif() {
        codigoTF.setEditable(false);
        nombreTF.setEditable(true);
        marcaTF.setEditable(true);

        ProveedoresJL.setEnabled(true);
        agregarProveedorB.setVisible(true);
        eliminarProveedorB.setVisible(true);

        LotesJL.setEnabled(true);

        cancelarB.setVisible(true);
        guardarB.setVisible(true);
    }

    public void visualizar(Producto producto) {
        codigoTF.setText("" + producto.getCodigo());
        nombreTF.setText(producto.getNombre());
        marcaTF.setText(producto.getMarca());
        cantidadS.setText("" + producto.getCantidad());

        listaLotes = producto.getLotes();
        actualizarLotesJL();
        listaProveedores = producto.getProveedores();
        actualizarProveedoresJL();

    }

    public void limpiar() {
        codigoTF.setText("");
        nombreTF.setText("");
        marcaTF.setText("");
        cantidadS.setText("0");

        listaLotes = new ArrayList<>();
        actualizarLotesJL();
        listaProveedores = new ArrayList<>();
        actualizarProveedoresJL();
    }

    public void actualizarProveedoresJL() {
        ProveedoresJL.setModel(new AbstractListModel() {
            @Override
            public int getSize() {
                return listaProveedores.size();
            }

            @Override
            public Object getElementAt(int index) {
                return listaProveedores.get(index);
            }
        });
    }

    public void actualizarLotesJL() {
        LotesJL.setModel(new AbstractListModel() {
            @Override
            public int getSize() {
                return listaLotes.size();
            }

            @Override
            public Object getElementAt(int index) {
                return listaLotes.get(index);
            }
        });
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        codigoL = new javax.swing.JLabel();
        nombreL = new javax.swing.JLabel();
        codigoTF = new javax.swing.JTextField();
        marcaTF = new javax.swing.JTextField();
        cantidadL = new javax.swing.JLabel();
        panelSeleccion = new javax.swing.JPanel();
        panelLotes = new javax.swing.JPanel();
        lotesL = new javax.swing.JLabel();
        spLotes = new javax.swing.JScrollPane();
        LotesJL = new javax.swing.JList();
        panelProveedores = new javax.swing.JPanel();
        proveedoresL = new javax.swing.JLabel();
        panelBotonesProveedores = new javax.swing.JPanel();
        agregarProveedorB = new javax.swing.JButton();
        eliminarProveedorB = new javax.swing.JButton();
        spProveedores = new javax.swing.JScrollPane();
        ProveedoresJL = new javax.swing.JList();
        codigoL1 = new javax.swing.JLabel();
        nombreTF = new javax.swing.JTextField();
        cantidadS = new javax.swing.JLabel();
        guardarB = new javax.swing.JButton();
        cancelarB = new javax.swing.JButton();
        mensajeL = new javax.swing.JLabel();

        codigoL.setFont(new java.awt.Font("Segoe UI", 1, 12)); // NOI18N
        codigoL.setText("Código");

        nombreL.setFont(new java.awt.Font("Segoe UI", 1, 12)); // NOI18N
        nombreL.setText("Marca");

        codigoTF.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N

        marcaTF.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N

        cantidadL.setFont(new java.awt.Font("Segoe UI", 1, 12)); // NOI18N
        cantidadL.setText("Cantidad");

        panelSeleccion.setLayout(new java.awt.GridLayout(1, 0));

        panelLotes.setLayout(new java.awt.BorderLayout());

        lotesL.setFont(new java.awt.Font("Segoe UI", 1, 12)); // NOI18N
        lotesL.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lotesL.setText("Lotes");
        panelLotes.add(lotesL, java.awt.BorderLayout.PAGE_START);

        LotesJL.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "-" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        spLotes.setViewportView(LotesJL);

        panelLotes.add(spLotes, java.awt.BorderLayout.CENTER);

        panelSeleccion.add(panelLotes);

        panelProveedores.setLayout(new java.awt.BorderLayout());

        proveedoresL.setFont(new java.awt.Font("Segoe UI", 1, 12)); // NOI18N
        proveedoresL.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        proveedoresL.setText("Proveedores");
        panelProveedores.add(proveedoresL, java.awt.BorderLayout.PAGE_START);

        panelBotonesProveedores.setLayout(new java.awt.GridLayout(1, 0));

        agregarProveedorB.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/plusprov1.png"))); // NOI18N
        agregarProveedorB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregarProveedorBActionPerformed(evt);
            }
        });
        panelBotonesProveedores.add(agregarProveedorB);

        eliminarProveedorB.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/BO30.png"))); // NOI18N
        eliminarProveedorB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eliminarProveedorBActionPerformed(evt);
            }
        });
        panelBotonesProveedores.add(eliminarProveedorB);

        panelProveedores.add(panelBotonesProveedores, java.awt.BorderLayout.SOUTH);

        ProveedoresJL.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "-" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        spProveedores.setViewportView(ProveedoresJL);

        panelProveedores.add(spProveedores, java.awt.BorderLayout.CENTER);

        panelSeleccion.add(panelProveedores);

        codigoL1.setFont(new java.awt.Font("Segoe UI", 1, 12)); // NOI18N
        codigoL1.setText("Nombre");

        nombreTF.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N

        cantidadS.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        cantidadS.setText("0");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(panelSeleccion, javax.swing.GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(nombreL, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(cantidadL, javax.swing.GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE)
                            .addComponent(codigoL1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(codigoL, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(nombreTF, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(codigoTF)
                            .addComponent(marcaTF)
                            .addComponent(cantidadS, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(codigoTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(codigoL))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nombreTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(codigoL1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(marcaTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(nombreL))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cantidadL)
                    .addComponent(cantidadS))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelSeleccion, javax.swing.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE))
        );

        guardarB.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        guardarB.setText("Guardar");
        guardarB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guardarBActionPerformed(evt);
            }
        });

        cancelarB.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        cancelarB.setText("Cancelar");
        cancelarB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarBActionPerformed(evt);
            }
        });

        mensajeL.setFont(new java.awt.Font("Segoe UI Semibold", 0, 14)); // NOI18N
        mensajeL.setForeground(new java.awt.Color(255, 51, 0));
        mensajeL.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        mensajeL.setText("Mensaje");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(mensajeL, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(guardarB, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cancelarB)))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(mensajeL)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(guardarB)
                    .addComponent(cancelarB))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void cancelarBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarBActionPerformed
        limpiar();
        deshabilitarTodo();
        administrarProductos.actualizarResultados();

    }//GEN-LAST:event_cancelarBActionPerformed

    private void guardarBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardarBActionPerformed

        if(!VentanaPrincipal.esLong(codigoTF.getText())){
            mostrarAviso("El campo código solo admite valores numéricos", Color.RED);
            return;
        }
        
        Producto nuevo = new Producto(Long.parseLong(codigoTF.getText()),
                    nombreTF.getText(),
                    marcaTF.getText(),
                    Integer.parseInt(cantidadS.getText()),
                    listaProveedores);
        
        String mensaje = "";
        if (administrarProductos.getSeleccionado() == null) {
            mensaje = VentanaPrincipal.controlProducto.agregar(nuevo);
            if (!mensaje.equals("Producto agregado exitosamente!")){
                mostrarAviso(mensaje, Color.RED);
                return;
            }
        } else {
            mensaje = VentanaPrincipal.controlProducto.modificar(nuevo);
            if (!mensaje.equals("Producto modificado exitosamente!")){
                mostrarAviso(mensaje, Color.RED);
                return;
            }
        }
        
        mostrarAviso(mensaje, Color.BLUE);
        limpiar();
        deshabilitarTodo();
        administrarProductos.actualizarResultados();
    }//GEN-LAST:event_guardarBActionPerformed

    private void eliminarProveedorBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eliminarProveedorBActionPerformed
        listaProveedores.remove(ProveedoresJL.getSelectedIndex());
        actualizarProveedoresJL();
    }//GEN-LAST:event_eliminarProveedorBActionPerformed

    private void agregarProveedorBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_agregarProveedorBActionPerformed
        new SeleccionarProveedor(administrarProductos.getMainFrame(), true, this).setVisible(true);
        //new SeleccionarProveedor(administarProductos.getParent().getParent(), true, this).setVisible(true);
    }//GEN-LAST:event_agregarProveedorBActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList LotesJL;
    private javax.swing.JList ProveedoresJL;
    private javax.swing.JButton agregarProveedorB;
    private javax.swing.JButton cancelarB;
    private javax.swing.JLabel cantidadL;
    private javax.swing.JLabel cantidadS;
    private javax.swing.JLabel codigoL;
    private javax.swing.JLabel codigoL1;
    private javax.swing.JTextField codigoTF;
    private javax.swing.JButton eliminarProveedorB;
    private javax.swing.JButton guardarB;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lotesL;
    private javax.swing.JTextField marcaTF;
    private static javax.swing.JLabel mensajeL;
    private javax.swing.JLabel nombreL;
    private javax.swing.JTextField nombreTF;
    private javax.swing.JPanel panelBotonesProveedores;
    private javax.swing.JPanel panelLotes;
    private javax.swing.JPanel panelProveedores;
    private javax.swing.JPanel panelSeleccion;
    private javax.swing.JLabel proveedoresL;
    private javax.swing.JScrollPane spLotes;
    private javax.swing.JScrollPane spProveedores;
    // End of variables declaration//GEN-END:variables

    public void agregarProveedorAProducto(Proveedor p) {
        if (!listaProveedores.contains(p)) {
            listaProveedores.add(p);
            actualizarProveedoresJL();
        }
    }

    public static void mostrarAviso(String mensaje) {
        mensajeL.setText(mensaje);
        mensajeL.setForeground(Color.red);
    }

    public static void mostrarAviso(String mensaje, java.awt.Color s) {
        mensajeL.setText(mensaje);
        mensajeL.setForeground(s);
    }
}
